import { _decorator, Label, Node, tween, Vec3, } from 'cc';

export default class Utils {
    /** 目标节点做呼吸缩放动画 */
    static AutoScale(target: Node, scaleAmount: number = 0.1) {
        tween(target)
            .by(1, { scale: new Vec3(scaleAmount, scaleAmount, 0) })
            .by(1, { scale: new Vec3(-scaleAmount, -scaleAmount, 0) })
            .union()
            .repeatForever()
            .start();
    }

    /** 目标节点做上下浮动动画 */
    static AutoUpDown(target: Node) {
        tween(target)
            .to(0.5, { position: new Vec3(target.position.x, target.position.y + 10, target.position.z) })
            .to(0.5, { position: new Vec3(target.position.x, target.position.y, target.position.z) })
            .union()
            .repeatForever()
            .start();
    }

    /** 目标节点做压扁拉伸动画 */
    static AutoSquashStretch(target: Node) {
        tween(target)
            .to(0.5, { scale: new Vec3(1.2, 0.8, 1) })
            .to(0.5, { scale: new Vec3(0.8, 1.2, 1) })
            .union()
            .repeatForever()
            .start();
    }

    /** 目标节点做旋转动画 */
    static AutoRotate(target: Node, speed: number = 1, angle: number = 30) {
        // 以Z轴旋转360度
        const defaultAngle = target.angle;
        tween(target)
            .to(speed, { angle: angle })
            .to(speed, { angle: defaultAngle })
            .union()
            .repeatForever()
            .start();
    }

    /** 数字从begin到end变化的动画 */
    static startAddLable(label: Label, begin: number, end: number, duration: number, cb: Function = null) {
        label.node.active = true;
        label.string = `${begin}`;
        let obj = { value: begin };
        tween(obj)
            .to(duration, { value: end }, {
                onUpdate: () => { label.string = `${Math.round(obj.value)}`; }
            })
            .call(() => { cb?.(); })
            .start();
    }
}


